package com.i2f.i2msk.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import com.i2f.i2msk.Keyboard;
import com.i2f.i2msk.enums.KeyboardEnum;
import com.i2f.i2msk.img.KeyboardImg;

public class StandardKeyboard extends Keyboard {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2202273499641819869L;
	List<Integer> randomNumberKey;
	List<Character> randomyLetterKey;
	List<Character> randomXLetterKey;
	List<Character> randomSymbolKey;
	int index;
	public StandardKeyboard(KeyboardEnum keyboardEnum) {
		super(keyboardEnum);
		this.randomNumberKey = new ArrayList<Integer>(numberKey);
		this.randomyLetterKey = new ArrayList<Character>(yLetterKey);
		this.randomXLetterKey = new ArrayList<Character>(XLetterKey);
		this.randomSymbolKey = new ArrayList<Character>(symbolKey);
		
		if(keyboardEnum == KeyboardEnum.STANDARD_DISORDER_RSA || keyboardEnum == KeyboardEnum.STANDARD_DISORDER_SM2 ){
			Collections.shuffle(randomNumberKey);
			Collections.shuffle(randomyLetterKey);
			Collections.shuffle(randomXLetterKey);
			Collections.shuffle(randomSymbolKey);
		}
		
		index=new Random().nextInt(randomSymbolKey.size()-7);
	}

	@Override
	public String getKeyboardImage()  {
		try {
			List<Character> tmp=symbolKey.subList(index, index+7);
			StringBuffer sb=new StringBuffer();
			sb.append(KeyboardImg.createLetterKeyboard(randomyLetterKey, 'y'));
			sb.append(";$;");
			String s= encryptionUtil.encoder(randomyLetterKey);
			sb.append(s);
			sb.append(";$;");
			sb.append(KeyboardImg.createLetterKeyboard(randomXLetterKey, 'X'));
			sb.append(";$;");
			sb.append(encryptionUtil.encoder(randomXLetterKey));
			sb.append(";$;");
			sb.append(KeyboardImg.createExtendNumberKeyboard(randomNumberKey, tmp));
			sb.append(";$;");
			sb.append(encryptionUtil.encoder(randomNumberKey));
			sb.append(encryptionUtil.encoder(tmp));
			sb.append(";$;");
			sb.append(KeyboardImg.createSymbolKeyboard(randomSymbolKey));
			sb.append(";$;");
			sb.append(encryptionUtil.encoder(randomSymbolKey));
			return sb.toString();
		} catch (IOException e) {
			throw new RuntimeException("键盘图片文件错误。无法读取",e);
		}
	}
}
